//
//  ARTAppUpdateView.m
//  ChartsDemo
//
//  Created by yunda on 2021/10/15.
//

#import "ARTAppUpdateView.h"
#import "AppDelegate.h"

@implementation ARTAppUpdateView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-  (instancetype)initWithTitle:(NSString *)title message:(NSString *)message{
    self = [super init];
    if (!self) {
        return nil;
    }
    // init code
    self.frame = CGRectMake(0, 0, J_SCREEN_WIDTH, J_SCREEN_HEIGHT);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.1];
    
    CGFloat gap = 40;
    CGFloat w = J_SCREEN_WIDTH-gap*2;
    CGFloat h = w*1.2;
    self.viewBg.frame = CGRectMake(gap, (J_SCREEN_HEIGHT-h)/2, w, h);
    [self addSubview:self.viewBg];
    self.viewBg.backgroundColor = [UIColor clearColor];
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    [self.viewBg addSubview:view];
    
    [self.viewBg addSubview:self.imageViewbg];
    
    [_imageViewbg addSubview:self.imageLabel];
        
    [view addSubview:self.titleLabel];
    [view addSubview:self.detailLabel];
    [view addSubview:self.cancleButton];
    [view addSubview:self.okButton];
    
    _imageViewbg.frame = J_Frame(0, 0, self.viewBg.v_w, self.viewBg.v_w/2.7);
    _imageLabel.frame = J_Frame(20, 60, _imageViewbg.v_w-20*2, 20);
    view.frame = J_Frame(0, _imageViewbg.v_MaxY-_imageViewbg.v_h/2, self.viewBg.v_w, 20);
    _titleLabel.frame = J_Frame(20, _imageViewbg.v_h/2+16, view.v_w-20*2, 20);
    _detailLabel.frame = J_Frame(_titleLabel.v_x, _titleLabel.v_MaxY+16, _titleLabel.v_w, 20);
    
//    _titleLabel.text = SAFE_STRING(title);
    _detailLabel.text = SAFE_STRING(message);
    CGSize detailSize = [_detailLabel sizeThatFits:CGSizeMake(CGRectGetWidth(_detailLabel.frame), CGFLOAT_MAX)];
    _detailLabel.v_h = detailSize.height;
    
    CGFloat buttonW = view.v_w/2;
    _cancleButton.frame = J_Frame(0, _detailLabel.v_MaxY+30, buttonW, 50);
    _okButton.frame = J_Frame(_cancleButton.v_MaxX, _cancleButton.v_y, buttonW, _cancleButton.v_h);
    
    UIView *line1 = [[UIView alloc]initWithFrame:J_Frame(_cancleButton.v_x,_cancleButton.v_y,view.v_w,0.5)];
    line1.backgroundColor = J_ColorFromHEX(0xF1F1F1);
    [view addSubview:line1];
    
    UIView *line2 = [[UIView alloc]initWithFrame:J_Frame(buttonW,_cancleButton.v_y,0.6,_cancleButton.v_h)];
    line2.backgroundColor = J_ColorFromHEX(0xF1F1F1);
    [view addSubview:line2];
    
    view.v_h = _cancleButton.v_MaxY;
    self.viewBg.frame = CGRectMake(gap, (J_SCREEN_HEIGHT-h)/2, w, view.v_MaxY);
    
    J_ViewRadius(view, 5);
    
    self.animationType = PopAnimationType_Center;
    return self;
}

- (void)show{
    AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    UIWindow *window = app.window;
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [app.window addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        self.viewBg.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}

- (void)cancleAction{
    [self dismiss];
}

- (void)okAction{
    [self dismiss];
    if (self.okBlock) {
        self.okBlock();
    }
}

- (UIImageView *)imageViewbg{
    if (!_imageViewbg) {
        _imageViewbg = [[UIImageView alloc]initWithImage:J_ImageNamed(@"gengxin")];
    }
    return _imageViewbg;
}

- (UILabel *)imageLabel{
    if (!_imageLabel) {
        _imageLabel = [[UILabel alloc]init];
        _imageLabel.textColor = J_ColorFromHEX(0xFFFFFF);
        _imageLabel.font = j_SemiboldFont(19);
        _imageLabel.text = FYLocalizedString(@"update", @"");
        _imageLabel.textAlignment = NSTextAlignmentRight;
    }
    return _imageLabel;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = J_ColorFromHEX(0x333333);
        _titleLabel.font = j_SemiboldFont(17);
        _titleLabel.text = FYLocalizedString(@"update", @"");
    }
    return _titleLabel;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc]init];
        _detailLabel.textColor = J_ColorFromHEX(0x666666);
        _detailLabel.font = J_Font(15);
        _detailLabel.numberOfLines = 0;
    }
    return _detailLabel;
}

- (UIButton *)cancleButton{
    if (!_cancleButton) {
        _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancleButton setTitleColor:J_ColorFromHEX(0x333333) forState:UIControlStateNormal];
        [_cancleButton setTitle:FYLocalizedString(@"oxygen_top_cancel", @"") forState:UIControlStateNormal];
        [_cancleButton addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleButton;
}

- (UIButton *)okButton{
    if (!_okButton) {
        _okButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_okButton setTitleColor:J_ColorFromHEX(0x0095B6) forState:UIControlStateNormal];
        [_okButton setTitle:FYLocalizedString(@"oxygen_top_determine", @"") forState:UIControlStateNormal];
        [_okButton addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okButton;
}

@end
